package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.cn.kehong.api.ISysSupplierService;
import com.cn.kehong.domain.OeeVo;
import com.cn.kehong.domain.SysSupplier;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

//供方管理
@Controller
@RequestMapping("/supplier")
public class SysSupplierController {

    @Autowired
    private ISysSupplierService iSysSupplierService;

    //初始化供方管理列表页面
    @RequestMapping("/getSupplier/{frontToken}")
    @ResponseBody
    public JsonResponse<List<SysSupplier>> getSupplier(@PathVariable(value = "frontToken", required = true) String frontToken,  QueryForm queryForm) throws Exception {
        JsonResponse<List<SysSupplier>> response = new JsonResponse<>();
        List<SysSupplier> sysSuppliers = iSysSupplierService.getSupplier(queryForm);
        response.setResult(sysSuppliers);
        response.setSuccess(true);
        return response;
    }


    //供方管理新增接口
    @RequestMapping("/addSupplier/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addSupplier(SysSupplier sysSupplier, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iSysSupplierService.addSupplier(sysSupplier);
        return CommonUtil.getResponseData(response,result);
    }




     //供方管理删除接口
    @RequestMapping("/deleteSupplier/{frontToken}")
    @ResponseBody
    public JsonResponse<String> deleteSupplier(@RequestBody QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iSysSupplierService.deleteSupplier(queryForm);
        return CommonUtil.getResponseData(response,result);
    }


    //供方管理修改接口
    @RequestMapping("/updateSupplier/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateSupplier(SysSupplier sysSupplier, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = iSysSupplierService.updateOeeOrganization(sysSupplier);
        return CommonUtil.getResponseData(response,result);
    }


    //供方管理下载接口
    @ResponseBody
    @RequestMapping("/exportSupplier")
    public void exportSupplier(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
        queryStr = URLDecoder.decode(queryStr,"utf-8");
        queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
        JSONObject jsonObject = JSON.parseObject(queryStr);
        queryForm.setSupplierName(jsonObject.getString("supplierName"));
        queryForm.setStatus(jsonObject.getString("status"));
        }
        iSysSupplierService.exportSupplier(queryForm,response);
    }


}
